package com.adlotto2.lcdns.activity.info;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.common.BaseActivity;

public class LottoAdInfoRecommenderActivity extends BaseActivity {
	
	private Context mContext;
	private final String BASE_URL = "http://ad.lcdns.kr/server/findManager.asp?ManagerId=";
	private final String RESULT_SUCCESS = "SUCCESS";
	private final String RESULT_FAIL = "FAIL";
	
	private Button mDoneButton, mOkButton;
	private EditText mRecommenderId;
	
	// 추천인 ID 확인이 됐는지 여부 체크하는 flag
	private boolean mIsRecommenderIdValidated = false;
	// 확인된 추천인 ID를 저장하는 String
	private String mValidatedId = "";
	
	
	@Override
	protected int onGetContentViewResource() {
		return R.layout.lottoad_info_recommender;
	}
	
	
	@Override
	protected void onInit() {
		setContentView(R.layout.lottoad_info_recommender);
		
		mContext = this;
		
		initViews();
	}
	
	public void initViews() {
		mDoneButton = (Button) findViewById(R.id.btn_recommender_done);
		mOkButton = (Button) findViewById(R.id.btn_recommender_ok);
		mRecommenderId = (EditText) findViewById(R.id.et_recommender_number);
		
		mDoneButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onDone();
			}
		});
		
		mOkButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onDone();
			}
		});
		
	}
	
	public void onDone() {
		// 20121210 추천인 입력하지 않은경우 진행할 수 없도록 바뀜.
		if(mRecommenderId.getText().length() == 0) {
			// 추천인 입력하지 않은경우
			new AlertDialog.Builder(mContext).setTitle(R.string.popup_alert).
			setMessage(R.string.no_recommender_warning).
			setPositiveButton(R.string.btn_title_yes, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).show();
			
		} else {
			onRequestData();
		}
	}
	
	@Override
	public void onBackPressed() {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mRecommenderId.getWindowToken(), 0);
		
		super.onBackPressed();
	}
	
	public void finishWithResult() {
		
		Intent data = new Intent();
		if(mIsRecommenderIdValidated) {
			data.putExtra("recommenderId", mValidatedId);
		}
		setResult(RESULT_OK, data);
		finish();
	}

	@Override
	public void onAsyncPreRequest() {
		super.showLoadingDialog(R.string.checking);
	}

	@Override
	public void onAsyncRequestCompleted(Element xmlObject) {
		super.hideLoadingDialog();
		parseJobList(xmlObject);
	}

	@Override
	public void onAsyncCancelRequest() {
		super.hideLoadingDialog();
	}

	@Override
	public String onGetAsyncRequestUrl() {
		return BASE_URL + "AL" + mRecommenderId.getText().toString();
	}

	@Override
	public void onAsyncRequestError(int paramInt) {
		super.hideLoadingDialog();
	}

	@Override
	public void onAsyncDataError(Element paramXMLObject, String paramString) {
		super.hideLoadingDialog();
	}
	
	private void parseJobList(Element xmlObject) {
		String result = RESULT_FAIL;
		try {
			NodeList returnCodeList = xmlObject.getElementsByTagName("rcode");
			
			result = returnCodeList.item(1).getFirstChild().getNodeValue();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		if(result.equals(RESULT_FAIL)) {
			showFailDialog();
		} else {
			if(result.equals(RESULT_SUCCESS)) {
				mIsRecommenderIdValidated = true;
				mValidatedId = "AL" + mRecommenderId.getText().toString();
				finishWithResult();
			}
		}
	}
	
	private void showFailDialog() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(R.string.popup_alert).
		setMessage(R.string.not_valid_recommender_id).
		setPositiveButton(R.string.commons_ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				mRecommenderId.setText("");
			}
		}).show();
	}
	
}
